﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.ViewModels.Pages
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Helpers;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;
    using Probel.Mvvm.Gui;

    public class SearchViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICriterionFactory CriterionFactory;
        private readonly IPatientService Service;
        private readonly ICommand simpleSearchCommand;

        private string criterion;
        private ExpandedPatientCardDto expandedPatient;
        private int searchCount = 0;
        private PatientCardDto selectedPatient;

        #endregion Fields

        #region Constructors

        public SearchViewModel(IPatientService service, IErrorService errorService, ICriterionFactory criterionFactory, bool isAsync = true)
            : base(errorService, isAsync)
        {
            if (service == null) { throw new ArgumentNullException("service"); }

            this.CriterionFactory = criterionFactory;
            this.Service = service;

            this.SearchResult = new ObservableCollection<PatientCardDto>();

            this.simpleSearchCommand = new RelayCommand(this.SimpleSearch);
        }

        #endregion Constructors

        #region Properties

        public string Criterion
        {
            get { return this.criterion; }
            set
            {
                this.criterion = value;
                this.OnPropertyChanged(() => Criterion);
            }
        }

        public ExpandedPatientCardDto ExpandedPatient
        {
            get { return this.expandedPatient; }
            set
            {
                this.expandedPatient = value;
                this.OnPropertyChanged(() => ExpandedPatient);
            }
        }

        public int SearchCount
        {
            get { return this.searchCount; }
            set
            {
                this.searchCount = value;
                this.OnPropertyChanged(() => SearchCount);
            }
        }

        public ObservableCollection<PatientCardDto> SearchResult
        {
            get;
            private set;
        }

        public PatientCardDto SelectedPatient
        {
            get { return this.selectedPatient; }
            set
            {
                this.selectedPatient = value;
                this.OnPropertyChanged(() => SelectedPatient);

                if (value != null)
                {
                    this.Async(() => this.Service.ExpandCard(value),
                                r => this.ExpandedPatient = r);
                }
            }
        }

        public ICommand SimpleSearchCommand
        {
            get { return this.simpleSearchCommand; }
        }

        #endregion Properties

        #region Methods

        private bool CanLoadPatient()
        {
            return this.SelectedPatient != null;
        }

        private void SimpleSearch()
        {
            if (string.IsNullOrEmpty(this.Criterion))
            {
                ViewService.MessageBox.Information(Messages.Msg_WarnEmptyCriterion);
            }
            else
            {
                Mouse.OverrideCursor = Cursors.Wait;
                this.SearchResult.Clear();

                this.Async(() => this.Service.GetPatients(CriterionFactory.Build(this.Criterion)),
                            t =>
                            {
                                this.SearchResult.Refill(t);
                                this.SearchCount = t.Count();
                                Mouse.OverrideCursor = Cursors.Arrow;
                            });
            }
        }

        #endregion Methods
    }
}